<?php
/**优惠券管理*/
namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Coupon extends Model
{
    //可用优惠券列表
    public static function coupon_useable($param)
    {
         $res = DB::table('coupon as cp')
             ->leftJoin('user_coupon as cpdt','cpdt.coupon_id','=','cp.coupon_id')
            ->where('cp.start_time','<',date('Y-m-d 59:59:59'))
            ->where('cp.end_time','>',date('Y-m-d 00:00:00'));
        if(isset($param['userid']) && $param['userid']!=''){
            $res=$res->where('cpdt.user_id',$param['userid']);
        }
        $res=$res->where('cpdt.status',1)
            ->get();
        return $res;
    }

    /**
     * 已使用优惠券列表(用户查询)
     * @param
    */
    public static function coupon_used($param)
    {
        $res = DB::table('coupon_detail as cpdt')
            ->leftJoin('coupon as cp','cpdt.couponid','=','cp.coupon_id');
        if(isset($param['orderid']) && $param['orderid']!=''){
            $res=$res->where('cpdt.orderid',$param['orderid']);
        }
        if(isset($param['userid']) && $param['userid']!=''){
            $res=$res->where('cpdt.userid',$param['userid']);
        }
        $res=$res->where('cpdt.status',1)
            ->get();
        return $res;
    }

    //过期优惠券列表
    public static function coupon_overuse($param)
    {
        $res = DB::table('coupon as cp')
            ->leftJoin('user_coupon as user_cp','user_cp.coupon_id','=','cp.coupon_id')
            ->where('user_cp.user_id',$param['userid'])
            ->where('cp.start_time','<',date('Y-m-d 59:59:59'))
            ->where('cp.end_time','<',date('Y-m-d 00:00:00'))
            ->select('cp.*','user_cp.status','user_cp.coupon_number')
            ->get();
        return $res;
    }

    public static function coupon_user($userid)
    {
        $res = DB::table('coupon as cp')
            ->leftJoin('user_coupon as user_cp','user_cp.coupon_id','=','cp.coupon_id')
            ->where('user_cp.user_id',$userid)
           // ->where('cp.start_time','<',date('Y-m-d 00:00:00'))
            //->where('cp.end_time','>',date('Y-m-d 00:00:00'))
            ->select('cp.*','user_cp.status','user_cp.coupon_number')
            ->get();
        return $res;
    }
}
